/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.i0o.appliancemanager.ui.window;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import org.i0o.appliancemanager.helper.UIHelper;
import org.i0o.appliancemanager.helper.Utilities;
import org.i0o.utilplus.ValidateUtil;

/**
 *
 * @author Administrator
 */
public class LoginWin extends BaseWin {

    /**
     * Creates new form LoginWin
     */
    public LoginWin() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        JPanel jPanel1 = new JPanel();
        lblTitle = new JLabel();
        JLabel lblUsername = new JLabel();
        txtUsername = new JTextField();
        JLabel lblPassword = new JLabel();
        txtPassword = new JPasswordField();
        JLabel jLabel4 = new JLabel();
        JComboBox jComboBox1 = new JComboBox();
        btnLogin = new JButton();
        btnCancel = new JButton();

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setTitle(Utilities.getString("loginTitle")); // NOI18N
        setResizable(false);

        lblTitle.setText(Utilities.getString("softwareTitle")); // NOI18N

        GroupLayout jPanel1Layout = new GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblTitle, GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(Alignment.LEADING)
            .addGroup(Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblTitle, GroupLayout.DEFAULT_SIZE, 58, Short.MAX_VALUE)
                .addContainerGap())
        );

        lblUsername.setText(Utilities.getString("loginLblUser")); // NOI18N

        txtUsername.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent evt) {
                txtUsernameKeyPressed(evt);
            }
        });

        lblPassword.setText(Utilities.getString("loginLblPassword")); // NOI18N

        txtPassword.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent evt) {
                txtPasswordKeyPressed(evt);
            }
        });

        jLabel4.setText(Utilities.getString("loginLblModel")); // NOI18N

        jComboBox1.setModel(new DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        btnLogin.setText(Utilities.getString("loginBtnLogin")); // NOI18N
        btnLogin.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
                btnLoginMouseClicked(evt);
            }
        });

        btnCancel.setText(Utilities.getString("loginLblCancel")); // NOI18N
        btnCancel.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
                btnCancelMouseClicked(evt);
            }
        });

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(Alignment.LEADING)
            .addComponent(jPanel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(151, 151, 151)
                .addGroup(layout.createParallelGroup(Alignment.LEADING)
                    .addGroup(Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnLogin, GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancel, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(Alignment.TRAILING)
                            .addComponent(jLabel4)
                            .addComponent(lblPassword)
                            .addComponent(lblUsername))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(Alignment.LEADING)
                            .addComponent(txtUsername, GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE)
                            .addComponent(txtPassword, GroupLayout.PREFERRED_SIZE, 1, Short.MAX_VALUE)
                            .addComponent(jComboBox1, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addGap(60, 60, 60))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(Alignment.BASELINE)
                    .addComponent(lblUsername)
                    .addComponent(txtUsername, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(Alignment.BASELINE)
                    .addComponent(lblPassword)
                    .addComponent(txtPassword, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jComboBox1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(Alignment.BASELINE)
                    .addComponent(btnLogin)
                    .addComponent(btnCancel))
                .addGap(20, 20, 20))
        );

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-420)/2, (screenSize.height-266)/2, 420, 266);
    }// </editor-fold>//GEN-END:initComponents

    /**
     * 登陆方法
     */
    private void login() {
        String username = this.txtUsername.getText().trim();
        char[] password = this.txtPassword.getPassword() ;
        if (username.length() == 0) {
            UIHelper.showErrorAlert(this, Utilities.getString("msgUserNull"));
            this.txtUsername.requestFocus();
            return;
        }
        
        log.debug("password string length:" + password.length );
        if (password.length  == 0) {
            UIHelper.showErrorAlert(this, Utilities.getString("msgPasswordNull"));
            this.txtPassword.requestFocus();
            return;
        }

        PosWin posWin = new PosWin();
        posWin.open();

        this.close();
    }

    @Override
    protected void initStyle() {
        UIHelper.setFont(this.lblTitle, Font.BOLD, 40);
    }

    private void txtUsernameKeyPressed(KeyEvent evt) {//GEN-FIRST:event_txtUsernameKeyPressed

        int keyCode = evt.getKeyCode();
        if (keyCode == 10) {
            this.login();
        }
    }//GEN-LAST:event_txtUsernameKeyPressed

    private void txtPasswordKeyPressed(KeyEvent evt) {//GEN-FIRST:event_txtPasswordKeyPressed
        int keyCode = evt.getKeyCode();
        if (keyCode == 10) {
            this.login();
        }
    }//GEN-LAST:event_txtPasswordKeyPressed

    private void btnCancelMouseClicked(MouseEvent evt) {//GEN-FIRST:event_btnCancelMouseClicked
        super.close();
    }//GEN-LAST:event_btnCancelMouseClicked

    private void btnLoginMouseClicked(MouseEvent evt) {//GEN-FIRST:event_btnLoginMouseClicked
        this.login();
    }//GEN-LAST:event_btnLoginMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    JButton btnCancel;
    JButton btnLogin;
    private JLabel lblTitle;
    JPasswordField txtPassword;
    JTextField txtUsername;
    // End of variables declaration//GEN-END:variables
}
